<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="包号" prop="packageNumber">
        <el-input
          v-model="queryParams.packageNumber"
          placeholder="请输入包号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="第一方" prop="firstParty">
        <el-input
          v-model="queryParams.firstParty"
          placeholder="请输入第一方"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="第二方" prop="secondParty">
        <el-input
          v-model="queryParams.secondParty"
          placeholder="请输入第二方"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="接口标识类型" prop="interfaceType">
        <el-select
          v-model="queryParams.interfaceType"
          placeholder="请选择接口标识类型"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="发布方" prop="publisher">
        <el-input
          v-model="queryParams.publisher"
          placeholder="请输入发布方"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="接收方" prop="receiver">
        <el-input
          v-model="queryParams.receiver"
          placeholder="请输入接收方"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="关键接口" prop="isKeyInterface">
        <el-select
          v-model="queryParams.isKeyInterface"
          placeholder="请选择关键接口"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['datastore:design-icm:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['datastore:design-icm:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="包号" align="center" prop="packageNumber" />
      <el-table-column label="第一方" align="center" prop="firstParty" />
      <el-table-column label="第二方" align="center" prop="secondParty" />
      <el-table-column label="接口标识类型" align="center" prop="interfaceType" />
      <el-table-column label="接口流水号" align="center" prop="interfaceSerialNumber" />
      <el-table-column label="发布方" align="center" prop="publisher" />
      <el-table-column label="接收方" align="center" prop="receiver" />
      <el-table-column label="关键接口" align="center" prop="isKeyInterface" />
      <el-table-column label="设计院" align="center" prop="designInstitute" />
      <el-table-column label="接口详细描述" align="center" prop="interfaceDescription" />
      <el-table-column label="CNCS转发" align="center" prop="cncsForwarding" />
      <el-table-column
        label="初版计划提交时间"
        align="center"
        prop="initialPlannedSubmissionDate"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column
        label="终版计划提交时间"
        align="center"
        prop="finalPlannedSubmissionDate"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column
        label="终版计划关闭时间"
        align="center"
        prop="finalPlannedClosureDate"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="CNCS负责人" align="center" prop="cncsPersonInCharge" />
      <el-table-column label="IITF/IICS" align="center" prop="iitfIics" />
      <el-table-column label="版本" align="center" prop="version" />
      <el-table-column label="状态" align="center" prop="status" />
      <el-table-column
        label="信息交换日期"
        align="center"
        prop="informationExchangeDate"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="信息交换传递单号" align="center" prop="informationExchangeDeliveryNumber" />
      <el-table-column label="参考信函" align="center" prop="referenceLetter" />
      <el-table-column label="发文方" align="center" prop="sendingParty" />
      <el-table-column label="收文方" align="center" prop="receivingParty" />
      <el-table-column label="所答复传递单" align="center" prop="responseDeliveryNote" />
      <el-table-column label="审查意见" align="center" prop="reviewComments" />
      <el-table-column label="附件名称" align="center" prop="attachmentName" />
      <el-table-column
        label="内部要求答复日期"
        align="center"
        prop="internalResponseDueDate"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="预警值" align="center" prop="warningValue" />
      <el-table-column label="答复负责人" align="center" prop="responsePersonInCharge" />
      <el-table-column label="是否关闭" align="center" prop="isClosed" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="操作" align="center" min-width="120px">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['datastore:design-icm:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['datastore:design-icm:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <DesignIcmForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { DesignIcmApi, DesignIcmVO } from '@/api/datastore/designicm'
import DesignIcmForm from './DesignIcmForm.vue'

/** 接口管理 列表 */
defineOptions({ name: 'DesignIcm' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<DesignIcmVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  packageNumber: undefined,
  firstParty: undefined,
  secondParty: undefined,
  interfaceType: undefined,
  publisher: undefined,
  receiver: undefined,
  isKeyInterface: undefined,
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await DesignIcmApi.getDesignIcmPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await DesignIcmApi.deleteDesignIcm(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await DesignIcmApi.exportDesignIcm(queryParams)
    download.excel(data, '接口管理.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>